package com.hnkywl.sts.action.sfgl;

import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.sfgl.HzkxService;
import com.hnkywl.sts.service.sfgl.SkshService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by HL on 2015/9/22.
 */
@Namespace("/sfgl/hzkx")
public class HzkxAction extends SimpleActionSupport {

    private String sysDate;

    private Date sysdate = new Date();

    private HzkxService hzkxService;

    private SkshService skshService;

    //收款管理Id
    private Long hzkxId;

    private String zt;

    //单据号码
    private String bm;

    // 原单据编码
    private String ybm;

    //申报公司机构名称
    private String sbgs;

    //申报公司机构Id
    private Long sbgsId;

    //分页组件 起始位置
    private int page;

    //分页组件 页面大小
    private int rows = 10;

    //收付款实例
    private Sfk hzkx;

    private Long skgsId;

    private Long skzggsId;

    private Yg skyg;

    //功能操作
    private List<Gncz> gnczList;

    private GnczService gnczService;

    private String gldwMc;

    private GsjgService gsjgService;

    private Date hzkxEnd;

    private Date hzkxStart;

    private String shrq;

    private String shjg;

    private Long hzsfkyt;

    private Integer fplx;

    private Double hzje;

    @Action("hzkxList")
    public void ygList() {
        this.renderJson(this.hzkxService.findAllHzkx(hzkxStart, hzkxEnd, bm, ybm, sbgs, sbgsId, gldwMc, zt, page, rows));
    }


    @Action("hzkx")
    public String sksh() {
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        sysDate = simple.format(new Date());
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }


    @Actions({@Action("hzkx_content"), @Action("hzkx_detail"), @Action("hzkx_sh_detail")})
    public String skshContent() {
        Yg yg = YgService.getCurrentYg();
        Long gsId = YgService.getCurrentYgGsId();
        Long zggsId = YgService.getCurrentYgZgGsId();
        //进入update页面
        if (hzkxId != null) {
            hzkx = skshService.findSkshDetail(hzkxId);
        } else {
            hzkx = new Sfk();
            hzkx.setSbr(yg);
            hzkx.setSbgs((Nbgs) gsjgService.findGsjgDetail(zggsId));
        }
        return SUCCESS;
    }

    @Action("hzkx_sh")
    public void hzkx_sh() {
        try {
            this.hzkxService.changHzkxStatus(hzkx.getId(), shjg);
            renderText("{\"statusCode\":\"200\", \"message\":\"款项划转审核完成！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    // 款项划转详细页面
    @Action("hzkx_kxhz")
    public String hzkx_kxhz() {
        hzkx = this.skshService.findSkshDetail(hzkxId);
        return SUCCESS;
    }

    // 保存款项划转页面
    @Action("hzkx_kxhz_save")
    public void hzkx_kxhz_save() {
        try {
            String str = dealKhhz(hzkx);
            this.skshService.saveHzSksh(hzkx, hzsfkyt, hzje, fplx, str);
            renderText("{\"statusCode\":\"200\", \"message\":\"划转成功！\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 处理划转信息
     *
     * @param sksh
     * @return 0:只抛账不审核，1：只审核不抛账，2：既要审核又要抛账 ,3: 不抛账不审核
     */
    private String dealKhhz(Sfk sksh) {
        Long dlyId = 126591L; // 代理费
        Long hkId = 120591L; // 货款
        String isYBzj = "0"; // 原来款项是否保证金
        String isXbzj = "0"; // 划转后是否保证金
        if (!sksh.getKxsy().getId().equals(dlyId) && !sksh.getKxsy().getId().equals(hkId)) {
            isYBzj = "1";
        }
        if (!hzsfkyt.equals(dlyId) && !hzsfkyt.equals(hkId)) {
            isXbzj = "1";
        }
        if (sksh.getFplx() == 0 || (sksh.getFplx() == 1 && fplx == 1)) {

            // 原来是货款，划转后不是货款，需要抛账
            if (sksh.getKxsy().getId().equals(hkId)) {
                if (!hzsfkyt.equals(hkId)) {
                    return "0";
                }
            }

            // 原来不是货款，现在是货款
            if (!sksh.getKxsy().getId().equals(hkId)) {
                if (hzsfkyt.equals(hkId)) {
                    return "0";
                }
            }

            // 原来是代理费，划转后不是代理费，需要抛账
            if (sksh.getKxsy().getId().equals(dlyId)) {
                if (!hzsfkyt.equals(dlyId)) {
                    return "0";
                }
            }

            // 原来不是代理费，现在是代理费
            if (!sksh.getKxsy().getId().equals(dlyId)) {
                if (hzsfkyt.equals(dlyId)) {
                    return "0";
                }
            }
        } else {
            // 原来是货款
            if (sksh.getKxsy().getId().equals(hkId)) {
                if (!hzsfkyt.equals(hkId)) {
                    return "2";
                } else {
                    return "1";
                }
            }

            // 原来是代理费
            if (sksh.getKxsy().getId().equals(dlyId)) {
                if (!hzsfkyt.equals(dlyId)) {
                    return "2";
                } else {
                    return "1";
                }
            }

            // 原来是保证金
            if (isYBzj.equals("1")) {
                if (!isXbzj.equals("1")) {
                    return "2";
                } else {
                    return "1";
                }
            }
        }
        return "3";
    }
    //===============================

    public Date getSysdate() {
        return sysdate;
    }

    public void setSysdate(Date sysdate) {
        this.sysdate = sysdate;
    }


    public Long getHzkxId() {
        return hzkxId;
    }

    public void setHzkxId(Long hzkxId) {
        this.hzkxId = hzkxId;
    }

    public String getZt() {
        return zt;
    }

    public void setZt(String zt) {
        this.zt = zt;
    }

    public String getBm() {
        return bm;
    }

    public void setBm(String bm) {
        this.bm = bm;
    }

    public Long getSbgsId() {
        return sbgsId;
    }

    public void setSbgsId(Long sbgsId) {
        this.sbgsId = sbgsId;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public String getSbgs() {
        return sbgs;
    }

    public void setSbgs(String sbgs) {
        this.sbgs = sbgs;
    }


    public Sfk getHzkx() {
        return hzkx;
    }

    public void setHzkx(Sfk hzkx) {
        this.hzkx = hzkx;
    }

    public Long getSkgsId() {
        return skgsId;
    }

    public void setSkgsId(Long skgsId) {
        this.skgsId = skgsId;
    }

    public Long getSkzggsId() {
        return skzggsId;
    }

    public void setSkzggsId(Long skzggsId) {
        this.skzggsId = skzggsId;
    }

    public Yg getSkyg() {
        return skyg;
    }

    public void setSkyg(Yg skyg) {
        this.skyg = skyg;
    }

    public String getShrq() {
        return shrq;
    }

    public void setShrq(String shrq) {
        this.shrq = shrq;
    }

    public Date getHzkxStart() {
        return hzkxStart;
    }

    public void setHzkxStart(Date hzkxStart) {
        this.hzkxStart = hzkxStart;
    }

    public Date getHzkxEnd() {
        return hzkxEnd;
    }

    public void setHzkxEnd(Date hzkxEnd) {
        this.hzkxEnd = hzkxEnd;
    }

    public String getGldwMc() {
        return gldwMc;
    }

    public void setGldwMc(String gldwMc) {
        this.gldwMc = gldwMc;
    }

    public String getSysDate() {
        return sysDate;
    }

    public void setSysDate(String sysDate) {
        this.sysDate = sysDate;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public String getYbm() {
        return ybm;
    }

    public void setYbm(String ybm) {
        this.ybm = ybm;
    }

    public String getShjg() {
        return shjg;
    }

    public void setShjg(String shjg) {
        this.shjg = shjg;
    }

    public Long getHzsfkyt() {
        return hzsfkyt;
    }

    public void setHzsfkyt(Long hzsfkyt) {
        this.hzsfkyt = hzsfkyt;
    }

    public Integer getFplx() {
        return fplx;
    }

    public void setFplx(Integer fplx) {
        this.fplx = fplx;
    }

    public Double getHzje() {
        return hzje;
    }

    public void setHzje(Double hzje) {
        this.hzje = hzje;
    }

    @Autowired
    public void setHzkxService(HzkxService hzkxService) {
        this.hzkxService = hzkxService;
    }


    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }

    @Autowired
    public void setSkshService(SkshService skshService) {
        this.skshService = skshService;
    }
}
